package com.jobpass.controller;

import com.alibaba.excel.EasyExcel;
import com.jobpass.common.data.ResultData;
import com.jobpass.common.model.Grade;
import com.jobpass.common.model.Student;
import com.jobpass.common.service.GradeService;
import com.jobpass.common.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

@Slf4j
@Api(tags = "处理学生信息")
@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentServiceImpl;

    @Resource
    private GradeService gradeServiceImpl;

    /**
     * 根据学号查询学生信息
     * @param number
     * @return
     */
    @ApiOperation(value = "根据学号查询学生信息", notes = "必须有学生信息")
    @GetMapping("/queryInfo/{number}")
    private ResultData findStudentByNum(@PathVariable String number) {
        Student student = studentServiceImpl.findStudentByNum(number);
        Grade grade = gradeServiceImpl.findGradeName(student.getGradeId());
        student.setGradeName(grade.getName());
        return ResultData.success(student);
    }

    /**
     *
     * @param number
     * @param name
     * @param sex
     * @param age
     * @param identityCard
     * @param tel
     * @param mailbox
     * @param portrait
     * @param resume
     * @param enrolTime
     * @param gradeId
     * @return
     */
    @ApiOperation(value = "根据学号修改学生信息", notes = "必须有学生信息")
    @PutMapping("/updateInfo")
    private ResultData updateStudentByNum(@RequestParam(required = true) String number,
                                          @RequestParam(required = true) String name,
                                          @RequestParam(required = true) Integer sex,
                                          @RequestParam(required = true) Integer age,
                                          @RequestParam(required = true) String identityCard,
                                          @RequestParam(required = true) String tel,
                                          @RequestParam(required = true) String mailbox,
                                          @RequestParam(required = true) String portrait,
                                          @RequestParam(required = true) String resume,
                                          @RequestParam(required = true) String enrolTime,
                                          @RequestParam Long gradeId) {
        try {


            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.CHINESE);
            LocalDateTime parse = LocalDate.parse(enrolTime, formatter).atStartOfDay();

            Student student = new Student(null,number,null,name,sex,age,identityCard,tel,mailbox,portrait,resume,parse,gradeId,null,null,null,null,null);
            int num = studentServiceImpl.updateStudentByNum(student);
            if (num == 0) {
                return ResultData.fail();
            }
        } catch (Exception e) {
            log.debug("修改失败!异常信息:"+e);
            return ResultData.fail();
        }
        return ResultData.success();
    }

    /**
     * 根据学号删除学生信息
     * @param number
     * @return
     */
    @ApiOperation(value = "根据学号删除学生信息", notes = "必须有学生ID")
    @DeleteMapping("/delInfo/{number}")
    private ResultData delStudentByNum(@PathVariable String number){
        try {
            int num = studentServiceImpl.delStudentByNum(number);
            if (num==0){
                return ResultData.fail();
            }
        } catch (Exception e) {
            log.debug("删除失败!异常信息:"+e);
            return ResultData.fail();
        }
        return ResultData.success();
    }

    /**
     *
     * @param number
     * @param name
     * @param sex
     * @param age
     * @param identityCard
     * @param tel
     * @param mailbox
     * @param portrait
     * @param resume
     * @param enrolTime
     * @param gradeId 班级ID
     * @return
     */
    @ApiOperation(value = "添加学生信息", notes = "必须有学生信息")
    @PostMapping("/addInfo")
    private ResultData addStudent(@RequestParam(required = true) String number,
                                  @RequestParam(required = true) String name,
                                  @RequestParam(required = true) Integer sex,
                                  @RequestParam(required = true) Integer age,
                                  @RequestParam(required = true) String identityCard,
                                  @RequestParam(required = true) String tel,
                                  @RequestParam(required = true) String mailbox,
                                  @RequestParam(required = true) String portrait,
                                  @RequestParam(required = true) String resume,
                                  @RequestParam(required = true) String enrolTime,
                                  @RequestParam Long gradeId){
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.CHINESE);
            LocalDateTime parse = LocalDate.parse(enrolTime, formatter).atStartOfDay();


            Student student = new Student(null,number,null,name,sex,age,identityCard,tel,mailbox,portrait,resume,parse,gradeId,null,null,null,null,null);
            int num = studentServiceImpl.SavaStudent(student);
            if (num==0){
                return ResultData.fail();
            }
        } catch (Exception e) {
            log.debug("添加失败!异常信息:"+e);
            System.out.println(e);
            return ResultData.fail();
        }
        return ResultData.success();
    }


}